// business js
var basicAuthorizationData = '';

app
    // general rest service call, 'urladdress' must in ended with ?
    .service('ModeShapeService', function($q, $http, $state, $timeout, $window, $translate, $rootScope, Oauth2, Scopes, AuthService, RS, MS, toaster) {
        var modeShapeService = {};
        
        modeShapeService.pop = function(infoType, intoTitle, intoText, timeout){
        	if(timeout == null) {
        		timeout = 5000;
        	}
            toaster.pop(infoType, intoTitle, intoText, timeout);
            return toaster.toast.id;
        };
        modeShapeService.clear = function(id){
            // toaster.clear();
            toaster.close(id);
        };
        
        modeShapeService.getResponse = function(urladdress) {
        
        	/*
           	$http({
				  method: 'GET',
				  url: 'http://120.55.124.28:8080/modeshape-rest//referral/user/binary/ic/brentsun.jpg/jcr%3acontent/jcr%3adata?',
					headers: {
                	'authorization': 'Basic ' + btoa("brent.sun@gmail.com:password123"),
					'content-type': 'image/jped'
                }
				}).then(function successCallback(response) {
					 console.log('ModeShapeService successfully response=', response);
				  }, function errorCallback(response) {
					console.log('ModeShapeService error response=', response);
			  });
			 */
			 
            var rest_data = [];
            var refresh_token = '';

            var funcName = urladdress.split('\/')[2];
            urladdress = MS.protocol + MS.server + MS.port + MS.context + urladdress;

            return modeShapeService.getHttp(urladdress, funcName).then(function(data1) {
            	if(returnRes) {
            		rest_data = data1;
            	}else {
                    angular.forEach(data1.data, function(value, key) {
                        rest_data[key] = value;
                        //console.log(key + ': ' + value);
                    });
            	}
                return rest_data;
            }, function(data2) {
				modeShapeService.pop("error", "Resource Error", "Resource access error!");
            });
        };

        modeShapeService.getHttp = function(urladdress, funcName) {
            //console.log('RESTService getData urladdress=', urladdress);
            
            basicAuthorizationData = AuthService.loadData("_basicAuthorizationData");
            // $http.defaults.headers.common.Authorization = basicAuthorizationData;
            basicAuthorizationData = "Basic YnJlbnQuc3VuQGdtYWlsLmNvbTpwYXNzd29yZDEyMw==";
            modeShapeService.pop("success", "basicAuthorizationData", basicAuthorizationData);
            
            var deferred = $q.defer();
            $http({
                method: 'GET',
                dataType: 'jsonp',
                url: urladdress,
                headers: {
                	"Authorization": 'Basic ' + btoa("brent.sun@gmail.com:password123")
                }
            }).success(function(data, status, headers, config) {
                console.log('ModeShapeService successfully getData=', urladdress, data);
                deferred.resolve(data);
            }).error(function(data, status, headers, config) {
                //console.log('ModeShapeService error getData=', data);
                deferred.reject(data);
            });
            return (deferred.promise);
        };

        return modeShapeService;
    })
;